package com.cskaoyan.user.intercepter;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.annotation.Anonymous;
import com.cskaoyan.user.dto.CheckAuthRequest;
import com.cskaoyan.user.dto.CheckAuthResponse;
import com.cskaoyan.user.service.IUserService;
import jdk.nashorn.internal.ir.annotations.Reference;
import net.bytebuddy.implementation.bind.MethodDelegationBinder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.lang.reflect.Method;

import static com.alibaba.nacos.api.common.Constants.ACCESS_TOKEN;
import static com.alibaba.nacos.api.common.Constants.TOKEN;

/**
 * @program: duolaimall
 * @description:
 * @author: Wangtianhao
 * @create: 2022-05-19 11:46
 **/

public class TokenIntercepter extends HandlerInterceptorAdapter {

    @Autowired
    IUserService iUserService;

    public static String ACCESS_TOKEN = "access_token";

    public static String USER_INFO_KEY = "userInfo";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/*        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Object bean = handlerMethod.getBean();
        if(isAnonymous(handlerMethod)){
            return true;
        }

        String token = CookieUtil.getCookieValue(request,ACCESS_TOKEN);
        if(StringUtils.isEmpty(token)){
            ResponseData responseData = new ResponseUtil().setErrorMsg("token已失效");
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(JSON.toJSON(responseData).toString());
            return false;
        }

        //从token中获取用户信息
        CheckAuthRequest checkAuthRequest = new CheckAuthRequest();
        checkAuthRequest.setToken(token);
        CheckAuthResponse checkAuthResponse = iUserService.checkLogin(checkAuthRequest);
        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        if(checkAuthResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            request.setAttribute(USER_INFO_KEY,checkAuthResponse.getUserinfo());
            return HandlerInterceptor.super.preHandle(request,response,handler);
        }
        ResponseData responseData = new ResponseUtil().setErrorMsg(checkAuthResponse.getMsg());
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(JSON.toJSON(responseData).toString());*/
        return false;
    }

    private boolean isAnonymous(HandlerMethod handlerMethod){
        Object bean = handlerMethod.getBean();
        Class clazz = bean.getClass();
        if(clazz.getAnnotation(Anonymous.class)!=null){
            return true;
        }
        Method method = handlerMethod.getMethod();
        return method.getAnnotation(Anonymous.class)!=null;
    }
}
